package com.zut.movie.controller;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zut.movie.entity.CinemaData;
import com.zut.movie.entity.Movie;
import com.zut.movie.entity.vo.MovieVo;
import com.zut.movie.result.Result;
import com.zut.movie.service.MovieService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/movie")
@Slf4j
public class MovieController {
    @Autowired
    public MovieService movieService;

    @PostMapping
    public void insertMovie(String movie){

        JSONObject parseObj = JSONUtil.parseObj(movie);
        String data = parseObj.get("cinemaData").toString();
        Movie bean = JSONUtil.toBean(movie, Movie.class);
        CinemaData cinemaData = JSONUtil.toBean(data, CinemaData.class);
        bean.setCinemaData(cinemaData);
        log.info(bean.toString());
        movieService.insertMovie(bean);
    }


    @GetMapping("/all")
    public Result<List<MovieVo>> getAll(){
       List<MovieVo> list= movieService.getAll();
       return Result.success(list);
    }

    @DeleteMapping
    public Result<String> del(Long orderId ){
        log.info(orderId.toString());
        movieService.del(orderId);
        return Result.success();
    }
}
